package com.xunji.standard.controller;

import com.xunji.standard.common.R;
import com.xunji.standard.entity.StCase;
import com.xunji.standard.service.CaseService;
import com.xunji.standard.service.CaseShareService;
import com.xunji.standard.util.RequestContextUtil;
import com.xunji.standard.vo.rep.CaseShareUserResponse;
import com.xunji.standard.vo.req.CaseShareRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xuqiang_imyours
 * @Date: 2025/7/2 15:10
 * @Description:
 **/
@Slf4j
@Api(tags = "案件分享")
@RestController
@RequestMapping("/share")
public class CaseShareController {
    @Resource
    CaseShareService caseShareService;
    @Resource
    private CaseService caseService;

    // 案件分享
    @PostMapping("/case")
    @ApiOperation(value = "案件分享", notes = "案件分享")
    public R caseShare(@RequestBody CaseShareRequest request, HttpServletRequest serverRequest) {
        String userId = RequestContextUtil.getUser(serverRequest);
        String[] beUserIds = request.getBeUserIds();
        if (beUserIds.length == 0) {
            return R.failure("请选择分享人员");
        }
        caseShareService.add(request, userId);
        return R.success();
    }

    @GetMapping("/case/user/{caseId}")
    @ApiOperation(value = "已分享人员列表", notes = "已分享人员列表")
    public R caseShare(HttpServletRequest serverRequest, @PathVariable String caseId) {
        String userId = RequestContextUtil.getUser(serverRequest);
        ArrayList<CaseShareUserResponse> list = caseShareService.getListByUserId(userId, caseId);
        return R.success(list);
    }

    // 删除案件分享人员
    @DeleteMapping("/case/user/{shareId}")
    @ApiOperation(value = "删除案件分享人员", notes = "删除案件分享人员")
    public R deleteCaseShareUser(@PathVariable int shareId) {
        caseShareService.deleteById(shareId);
        return R.success();
    }

    // 已分享案件列表
    @GetMapping("/case")
    @ApiOperation(value = "已分享案件列表", notes = "已分享案件列表")
    public R caseShare(HttpServletRequest serverRequest) {
        String userId = RequestContextUtil.getUser(serverRequest);
        List<String> caseIds = caseShareService.selectCaseListByUserId(userId);
        List<StCase> cases = caseService.getCaseByIds(caseIds);
        return R.success(cases);
    }

    @DeleteMapping("/case/{caseId}")
    @ApiOperation(value = "删除分享的案件", notes = "删除分享的案件")
    public R deleteCaseShare(HttpServletRequest serverRequest, @PathVariable String caseId) {
        String userId = RequestContextUtil.getUser(serverRequest);
        caseShareService.deleteByCaseIdAndUserId(caseId,userId);
        return R.success();
    }
}
